package com.myexam.dao.impl;

import java.util.List;






import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.myexam.dao.SubjectDao;
import com.myexam.model.Subject;
import com.myexam.util.Page;
import com.myexam.util.PageUtil;

@Repository("subjectDao")
public class SubjectDaoimpl implements SubjectDao {
	@Autowired
	private HibernateTemplate hibernateTemplate;

	@Override
	public void addSubject(Subject subject) {
		// TODO Auto-generated method stub
		this.hibernateTemplate.save(subject);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> findSubjectByTitle(String subjectTitle) {
		// TODO Auto-generated method stub
		return (List<Subject>)this.hibernateTemplate.find("from Subject sub where sub.subjectTitle like ?",subjectTitle);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Subject> findSubjectByPage(final Page page) {
		// TODO Auto-generated method stub
		List<Subject> subjects = (List<Subject>)this.hibernateTemplate.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				String hql = "from Subject sub";
				List<Subject> list =  PageUtil.getList(session,hql,page);
				return list;
			}
		});
		return subjects;
	}
	

	@Override
	public int findSubjectCount() {
		// TODO Auto-generated method stub
		return this.hibernateTemplate.find("from Subject sub").size();
	}
	
	

	@Override
	public Subject findSubjectByID(int subjectID) {
		// TODO Auto-generated method stub
		return (Subject) this.hibernateTemplate.get(Subject.class,subjectID);
	}

	@Override
	public void updateSubject(Subject subject) {
		// TODO Auto-generated method stub
		this.hibernateTemplate.update(subject);
	}

	@Override
	public void deleteSubject(int subjectID) {
		// TODO Auto-generated method stub
		this.hibernateTemplate.delete(this.hibernateTemplate.load(Subject.class, subjectID));
	}

	/*@SuppressWarnings("unchecked")
	@Override
	public List<Subject> likeQueryByTitle( final Page page) {
		// TODO Auto-generated method stub
		List<Subject> subjects = this.hibernateTemplate.executeFind(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				String hql = "from Subject sub ";
				List<Subject> list = PageUtil.getList(session, hql,page);
				return list;
			}
		});
		return subjects;
	}*/

	/*@SuppressWarnings("unchecked")
	@Override
	public int findLikeQueryCount(String subjectTitle) {
		// TODO Auto-generated method stub
		List<Subject> subjects = this.hibernateTemplate.find("from Subject sub where sub.subjectTitle like ?","%"+subjectTitle+"%");
		return subjects.size();
	}*/

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Subject> randomFindSubject(final int number) {
		// TODO Auto-generated method stub
		List<Subject> subjects = (List<Subject>) this.hibernateTemplate.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				String hql = "from Subject sub order by rand()";
				List<Subject> list = PageUtil.getList(session, hql,number);
				return list;
			}
		});
		return subjects;
	}


}
